<template>
	<view class="container">
		<u--form ref="queryForm" :model="query" label-width="120rpx">
		  <u-form-item label="物料码">
		    <u--input 
		    	placeholder="请输入/扫描" 
		    	v-model.trim="query.labelNo" 
		    	suffixIcon="scan" 
		    	suffixIconStyle="font-size: 68rpx"
		    	style="flex: 1;"
					@confirm="extraInfo"
		    >
		    </u--input>
		  </u-form-item>
		</u--form>
		<u-toast ref="uToast"></u-toast>
		<view class="commonButton">
			<u-button :disabled="Object.keys(scanMaterial).length === 0" type="primary">确认绑定</u-button>
		</view>
	</view>
</template>

<script>
	import { lightShelfBindAPI } from '@/api/work/outbound.js'
	export default {
		data() {
			return {
				query: {
					labelNo: ''
				},
				scanMaterial: {}
			};
		},
		methods: {
			async lightShelfBind() {
				submitForm = {
					materialCode: this.scanMaterial.materialCode,
					materialReelId: this.scanMaterial.reelId
				}
				const res = await lightShelfBindAPI(submitForm)
				if (res.code === 200) {
					this.scanMaterial = {}
					this.query.labelNo = ''
					this.$refs.uToast.show({
						type: 'success',
						message: res.message
					})
				} else {
					return this.$refs.uToast.show({
						type: 'warning',
						message: res.message
					})
				}
			},
			extraInfo() {
				if (!this.query.labelNo.includes('*')) {
					this.query.labelNo = ''
					return this.$refs.uToast.show({
						type: 'warning',
						message: '请扫描正确的物料二维码'
					})
				}
				const array = this.query.labelNo.split('*')
				if (array.length !== 6) {
					this.query.labelNo = ''
					return this.$refs.uToast.show({
						type: 'warning',
						message: '请扫描正确的物料二维码'
					})
				}
				// 物料码 批次 日期 供应商 数量 reelId
				this.scanMaterial = {
					materialCode: array[0],
					materialBatch: array[1],
					date: array[2],
					supplier: array[3],
					materialQty: Number(array[4]),
					reelId: array[5]
				}
				this.query.labelNo = this.scanMaterial.reelId
			}
		}
	}
</script>

<style lang="scss">
.commonButton {
	width: 100%;
	display: flex;
	gap: 20rpx;
	position: fixed;
	left: 0;
	bottom: 0;
	box-sizing: border-box;
	padding: 15rpx;
	justify-content: space-between;
	background-color: #fff;
}
</style>
